package com.symria.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.views.jsp.StrutsBodyTagSupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.symria.framework.constant.ATTFrameWork;
import com.symria.framework.util.SpringUtil;
import com.symria.framework.util.StringUtil;
import com.symria.role.util.Logger;
import com.symria.server.bo.role.RoleService;
import com.symria.server.po.Constant;
import com.symria.system.constants.AppStatus;


public class ConstantsTag extends StrutsBodyTagSupport {
	private static final Logger logger = Logger.getLogger(ConstantsTag.class);
    private Object value;
    private String codename;
    private String code;
    private String type;
    private RoleService roleService;
    

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
	@Override
	public int doStartTag() throws JspException {
		if(StringUtil.equals("appInfo", type)){
			AppStatus[] status=AppStatus.values();
			
			StringBuffer sb=new StringBuffer();
			for(AppStatus a:status){
				
					sb.append("<option value=\"");
					sb.append(a.toString());
					if(value!=null){
						if(StringUtil.equals(a.toString(), value.toString())){
							sb.append("\" selected=\"selected\"");
						}
					}
					sb.append("\">");
					sb.append(a.toName());
					sb.append("</option>");
				
			}
			try {
				pageContext.getOut().write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			roleService = SpringUtil.getBean("roleService",RoleService.class);
		    List<Constant> list = null;
		    if(!StringUtil.isEmpty(codename)){
		    	String pcode = StringUtil.getProperties(codename);
		    	if(!StringUtil.isEmpty(pcode)){
		    		list = roleService.queryConstantsByPcode(pcode,ATTFrameWork.LOGIC_TRUE);
		    	}
		    }else if(!StringUtil.isEmpty(code)){
		    	list  = roleService.queryConstantsByPcode(code,ATTFrameWork.LOGIC_TRUE);
		    }else{
		    	list = new ArrayList<Constant>();
		    }
		    try {
				pageContext.getOut().write(createOptions(list));
			} catch (IOException e) {
				logger.logError("constants tag error:"+e.getMessage());
			}
		}
		
		return EVAL_BODY_BUFFERED;
	}
	
	
	
	
	public String createOptions(List<Constant> list){
		StringBuffer sBuf = new StringBuffer();
		for(Constant conts:list){
			sBuf.append("<option value=\""+conts.getCode()+"\"");
			if(value!=null){
			if(StringUtil.equals(conts.getCode(),value.toString())){
				sBuf.append("selected=\"selected\"");
			}
			}
			sBuf.append(">");
			sBuf.append(conts.getContsName());
			sBuf.append("</option>");
		}
		return sBuf.toString();
	}


	public String getCodename() {
		return codename;
	}
	public void setCodename(String codename) {
		this.codename = codename;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Object getValue() {
		return value;
	}
	
	public void setValue(Object value){
			try {
				if(value!=null){
				this.value = ExpressionEvaluatorManager.evaluate(
				        "value", value.toString(), Object.class, this, pageContext);
				}
			} catch (JspException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	
	
	
}
